/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.user;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.fbSession;
import model.fbUser;
import model.fbUserProfile;
import model.pojo.User;
import model.pojo.UserProfile;

/**
 *
 * @author Yasith
 */
public class getUserListing extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int startingFrom = 0;
        try {
            startingFrom = Integer.parseInt(request.getParameter("startingFrom"));
        } catch (NumberFormatException ex) {
        }
        int numberOfUsers = Integer.parseInt(this.getServletContext().getInitParameter("UserListingsUserCount"));
        int endingAt = startingFrom + numberOfUsers;
        
        List<User> users = fbUser.getUsers();
        List<User> sentUsers = null;

        if(users.size() < endingAt){
            endingAt = users.size();
            request.setAttribute("thereAreMorePages", false);
        }else{
            request.setAttribute("thereAreMorePages", true);
        }
        
        sentUsers = users.subList(startingFrom, endingAt);
        
        Map<User, UserProfile> user2userProfile = new HashMap<User, UserProfile>();
        for(User u: users){
            user2userProfile.put(u, fbUserProfile.getUserProfile(u.getUserID()));
        }
        
        request.setAttribute("users", sentUsers);
        request.setAttribute("user2userProfile", user2userProfile);
        request.setAttribute("endingAt", endingAt);
        
        if(request.getSession().getAttribute("fbSession")!=null){
         
            fbSession fbs = (fbSession)request.getSession().getAttribute("fbSession");
            request.setAttribute("currentUser", fbs.getUser());
        }
        
        request.getRequestDispatcher("userlisting.jsp").forward(request, response);
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
